<template>
  <div>
    <div>【大区数据信息】</div> 
    <div ref="target" class="w-full h-full"></div>
  </div>
</template>
<script lang="ts" setup>
import { ref, defineProps, onMounted, watch } from "vue";
import * as echarts from "echarts";
import type { EChartsOption, EChartsType } from "echarts"; // 导入类型
import type { Region, Regions } from "@/api/types";

const props = defineProps<{
  data: Regions;
}>();
console.log("横向柱状图", props.data);

//1. 根据DOM 实例，通过echarts.init方法，生成echarts实例
const target = ref<HTMLElement | null>(null);
let mChart: EChartsType;
onMounted(() => {
  mChart = echarts.init(target.value as HTMLElement);
  renderChart();
});

//2. 构建 options 配置对象，整个 echarts 的样式，皆有该对象决定

const renderChart = () => {
  console.log("横向柱状图", props.data);
  if (props.data.regions.length === 0) return;
  const options: EChartsOption = {
    //x轴展示数据
    xAxis: {
      show: false,
      type: "value",
      max: function (value: any) {
        return value.max * 1.2;
      },
    },
    yAxis: {
      type: "category",
      data: props.data.regions.map((item) => item.name),
      inverse: true,
      axisLine: { show: false }, //不展示横线
      axisTick: { show: false },//不展示刻度
      axisLabel: {
        color: '#9eb1c8',
        fontSize: 12,
      }
    },
    // 图表绘制的位置,上下左右
    grid: {
        top: 0,
        bottom: 0,
        left: 0,
        right: 0,
        containLabel: true
    },
    // 核心配置
    series: [{
        type: "bar",
        data: props.data.regions.map((item) => ({
                name: item.name,
                value: item.value,
                
        })),
        showBackground: true,
        backgroundStyle: {
            color: 'rgba(180,180,180,0.2)'
        },
        itemStyle:{
                color: '#479AD3',
                borderRadius: 5,
                shadowColor: 'rgba(0,0,0,0.3)',
                shadowBlur: 5,
        },
        barWidth: 10, // 柱子宽度
        label: {
            show: true,
            position: 'right',
            // textStyle: {
            //     color: '#fff'
            // },
            color: '#fff'
        }
    }],
  };
  //3 .最后通过 实例.set0ption 方法，设置配置对象   
  mChart.setOption(options);
};

watch(()=>props.data,renderChart)
    
</script>
<style scoped lang="scss"></style>
